package com.group4.Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;
import com.group4.DB.VideoComment;

@WebServlet(name = "DeleteComment", displayName = "DeleteComment", urlPatterns = { "/comment/delete" })
public class DeleteComment extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 190340105993846718L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream

		String commentId = request.getParameter("commentId");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();

		if (commentId == null) {
			jsonObject.addProperty("error", "missing parameter!");
			out.print(jsonObject);
			out.flush();
		} else {
			boolean res = VideoComment.deleteVideoComment(Integer
					.valueOf(commentId));
			if (res) {
				jsonObject.addProperty("success", "comment deleted");
				jsonObject.addProperty("commentId", commentId);
				out.print(jsonObject);
				out.flush();
			} else {
				jsonObject.addProperty("error",
						"Error occured while deleting comment!");
				out.print(jsonObject);
				out.flush();
			}

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String commentId = request.getParameter("commentId");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();

		if (commentId == null) {
			jsonObject.addProperty("error", "missing parameter!");
			out.print(jsonObject);
			out.flush();
		} else {
			boolean res = VideoComment.deleteVideoComment(Integer
					.valueOf(commentId));
			if (res) {
				jsonObject.addProperty("success", "comment deleted");
				jsonObject.addProperty("commentId", commentId);
				out.print(jsonObject);
				out.flush();
			} else {
				jsonObject.addProperty("error",
						"Error occured while deleting comment!");
				out.print(jsonObject);
				out.flush();
			}

		}
	}
}
